 <template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>
    
    <!-- 登录卡片 -->
    <div class="login-card">
      <div class="card-header">
        <div class="logo-container">
          <div class="logo-icon">
            <i class="el-icon-user-solid"></i>
          </div>
        </div>
        <h2 class="login-title">欢迎登录</h2>
        <p class="login-subtitle">请输入您的账号信息</p>
        
        <!-- SignalR状态指示器 -->
        <div v-if="showStatusIndicator" class="signalr-status-indicator">
          <div class="status-item">
            <div class="status-dot" :class="signalRStatus"></div>
            <span class="status-text">
              {{ signalRStatus === 'online' ? 'SignalR 在线' : 
                 signalRStatus === 'connecting' ? 'SignalR 连接中...' : 'SignalR 离线' }}
            </span>
          </div>
        </div>
      </div>
      
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="0"
        class="login-form"
      >
        <el-form-item prop="UserName">
          <el-input 
            v-model="ruleForm.UserName" 
            placeholder="请输入用户名"
            size="large"
            prefix-icon="User"
            class="custom-input"
          />
        </el-form-item>
        
        <el-form-item prop="Password">
          <el-input 
            v-model="ruleForm.Password" 
            show-password 
            placeholder="请输入密码"
            size="large"
            prefix-icon="Lock"
            class="custom-input"
          />
        </el-form-item>
        
        <el-form-item class="form-buttons">
          <el-button 
            type="primary" 
            @click="submitForm(ruleFormRef)"
            size="large"
            class="login-btn"
            :loading="loading"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
        
        <div class="form-footer">
          <el-button 
            @click="resetForm(ruleFormRef)" 
            type=""
            class="reset-btn"
          >
            重置表单
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { readBasic } from '@/Helper/axiosSetting'
import{useCounterStore} from '@/stores/counter'
import { useRouter } from 'vue-router'
import { signalRService } from '@/services/SignalRService'

const useinfo=useCounterStore();
const router=useRouter();
const loading = ref(false);

// SignalR状态管理
const signalRStatus = ref<'offline' | 'connecting' | 'online'>('offline');
const showStatusIndicator = ref(false);


const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  UserName: '',
  Password: '',
})

const rules = reactive<FormRules>({
  UserName: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
  ],
  Password: [
    {
      required: true,
      message: '密码不能为空',
      trigger: 'blur',
    },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  if (loading.value) return // 防止重复提交
  
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true;
      console.log('submit!')
      console.log('发送登录请求到:', import.meta.env.VITE_ReadAPP_API_URL);
      readBasic.get("api/Manage/Login",{params:{
        UserName:ruleForm.UserName,
        Password:ruleForm.Password
      }}).then(async res=>{
        loading.value = false;
        console.log('登录响应:', res.data);
        if(res.data.code==200){
            console.log('Token:', res.data.token);
            localStorage.setItem("token",res.data.token)
            useinfo.UserInfo.RidList=res.data.data.ridList
            useinfo.UserInfo.RoleName=res.data.data.roleName
            useinfo.UserInfo.Uid=res.data.data.id
            useinfo.UserInfo.UserName=res.data.data.userName
            useinfo.UserInfo.departId=res.data.data.departMentId
            
            // 显示状态指示器并开始连接
            showStatusIndicator.value = true;
            signalRStatus.value = 'connecting';
            
            // 登录成功后连接SignalR
            try {
              await signalRService.start()
              console.log('SignalR连接成功')
              
              // 加入用户组
              await signalRService.JoinGroup(res.data.data.userName)
              
              // 发送登录通知
              await signalRService.sendLoginNotifications(res.data.data.userName, ruleForm.Password)
              
              // 所有操作成功后设置为在线状态
              signalRStatus.value = 'online';
              console.log('SignalR完全连接并通知成功，状态：在线')
              
            } catch (error) {
              console.error('SignalR连接或通知失败:', error)
              signalRStatus.value = 'offline';
              // 即使SignalR失败也不影响登录流程
            }
            
            ElMessage.success(res.data.message)
            router.push({
                path:'/UserMenuView'
            })
        }else{
            ElMessage.error(res.data.message)
            // 登录失败时重置状态指示器
            showStatusIndicator.value = false;
            signalRStatus.value = 'offline';
        }
      }).catch(() => {
        loading.value = false;
        ElMessage.error('登录失败，请检查网络连接')
        // 网络错误时重置状态指示器
        showStatusIndicator.value = false;
        signalRStatus.value = 'offline';
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.circle-2 {
  width: 150px;
  height: 150px;
  top: 60%;
  right: 15%;
  animation-delay: 2s;
}

.circle-3 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 40px;
  width: 400px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  animation: slideIn 0.6s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.card-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-container {
  margin-bottom: 20px;
}

.logo-icon {
  width: 80px;
  height: 80px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
}

.logo-icon i {
  font-size: 40px;
  color: white;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
}

.login-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
}

/* SignalR状态指示器样式 */
.signalr-status-indicator {
  margin-top: 15px;
  padding: 12px 16px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 8px;
  border: 1px solid rgba(102, 126, 234, 0.2);
  backdrop-filter: blur(5px);
}

.status-item {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.status-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  transition: all 0.3s ease;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.3);
}

.status-dot.online {
  background: #52c41a;
  box-shadow: 0 0 0 2px rgba(82, 196, 26, 0.3), 0 0 8px rgba(82, 196, 26, 0.5);
  animation: pulse-online 2s infinite;
}

.status-dot.connecting {
  background: #faad14;
  box-shadow: 0 0 0 2px rgba(250, 173, 20, 0.3), 0 0 8px rgba(250, 173, 20, 0.5);
  animation: pulse-connecting 1s infinite;
}

.status-dot.offline {
  background: #ff4d4f;
  box-shadow: 0 0 0 2px rgba(255, 77, 79, 0.3);
}

.status-text {
  font-size: 13px;
  font-weight: 500;
  color: #333;
}

@keyframes pulse-online {
  0%, 100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.8;
  }
}

@keyframes pulse-connecting {
  0%, 100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.2);
    opacity: 0.6;
  }
}

.login-form {
  margin-top: 20px;
}

.custom-input :deep(.el-input__wrapper) {
  border-radius: 12px;
  border: 2px solid #e1e5e9;
  background: #f8f9fa;
  transition: all 0.3s ease;
  box-shadow: none;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: #667eea;
  background: white;
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.custom-input :deep(.el-input__inner) {
  height: 50px;
  font-size: 16px;
}

.form-buttons {
  margin-top: 30px;
}

.login-btn {
  width: 100%;
  height: 50px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
}

.form-footer {
  text-align: center;
  margin-top: 20px;
}

.reset-btn {
  color: #667eea;
  font-size: 14px;
  transition: all 0.3s ease;
}

.reset-btn:hover {
  color: #764ba2;
  transform: translateY(-1px);
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-card {
    width: 90%;
    padding: 30px 20px;
  }
  
  .login-title {
    font-size: 24px;
  }
  
  .logo-icon {
    width: 60px;
    height: 60px;
  }
  
  .logo-icon i {
    font-size: 30px;
  }
}
</style>
